import { createStore } from "vuex";
import { getSchoolList } from "../api/schoolApi";
import { ElMessage } from "element-plus";

function getInitialState() {
  return {
    user: null,
    schoolList: [],
  };
}

const store = createStore({
  state: getInitialState,
  mutations: {
    setUser(state, user) {
      state.user = user;
    },
    setSchoolList(state, schoolList) {
      state.schoolList = schoolList;
    },
    resetStore(state) {
      Object.assign(state, getInitialState());
    },
  },
  actions: {
    async fetchSchoolList(context) {
      try {
        const res = await getSchoolList();
        context.commit("setSchoolList", res);
      } catch (error) {
        ElMessage({
          message: error,
          type: "warning",
        });
      }
    },
  },
  getters: {
    count: (state) => state.count,
  },
});

export default store;
